function onStart() {
	if (localStorage.getItem("noOfCases") == null) {
		localStorage.setItem("noOfCases", "0");
	}
	if (localStorage.getItem("activeCases") == null) {
		localStorage.setItem("activeCases", "[]");
	}
}
	
	
 
	
function setCasesInAscendingOrder(){
    var i,j;
	var activeCases = JSON.parse(localStorage.getItem("activeCases"));
	var sortedCases = [];
	for(i=0;i<activeCases.length;i++){
			var caseObj = JSON.parse(localStorage.getItem(activeCases[i]));
			
			for(j=0;j<caseObj.dates.length;j++){
				var obj = JSON.parse('{"caseId":"","plaintiff":"","respondent":"","dateId":"","date":""}');
				var dateObj = caseObj.dates[j];
				obj.caseId = activeCases[i];
				obj.plaintiff = caseObj.plaintiff; 
				obj.respondent = caseObj.respondent; 
				obj.dateId = dateObj.dateId;
				obj.date = new Date(dateObj.caseDate);
				sortedCases.push(obj);
			}
			
	}
	
	
	var swapped;
    do {
        swapped = false;
        for (var i=0; i < sortedCases.length-1; i++) {
            if (sortedCases[i].date > sortedCases[i+1].date) {
			
                var temp = sortedCases[i];
                sortedCases[i] = sortedCases[i+1];
                sortedCases[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
	
	
	return sortedCases;
	
	
}

function upcomingCases(){
	var allCases = setCasesInAscendingOrder(),today = new Date(),i,result = [];
	var result = [];
	
	for(i=allCases.length-1;i>=0;i--){
		
		if((new Date(allCases[i].date.getFullYear(),allCases[i].date.getMonth(),allCases[i].date.getDate()+1)) >= today){
			result.push(allCases[i]);
		}
	}
	
	return result;
}

function bubbleSort(a)
{
    var swapped;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
}



function addCase(caseObj) {
	var noOfCases = localStorage.getItem("noOfCases");
	noOfCases = parseInt(noOfCases) + 1;
	localStorage.setItem("noOfCases", noOfCases.toString());
	localStorage.setItem(noOfCases.toString(), JSON.stringify(caseObj));
	var activeCases = JSON.parse(localStorage.getItem("activeCases"));
	activeCases.push(noOfCases);
	localStorage.setItem("activeCases", JSON.stringify(activeCases));

}

function getCase(caseId) {
	return JSON.parse(localStorage.getItem(caseId.toString()));
}

function updateCase(caseId, newCaseObj) {
	localStorage.setItem(caseId, JSON.stringify(newCaseObj));
}

function deleteCase(caseId) {
	localStorage.removeItem(caseId);
	var activeCases = JSON.parse(localStorage.getItem("activeCases"));
	for (var counter = 0; counter < activeCases.length; counter++) {
		if (activeCases[counter] == caseId) {
			activeCases.splice(counter, 1);
		}
	}
	localStorage.setItem("activeCases", JSON.stringify(activeCases));
}

function getDummyCaseObject() {
	return JSON.parse('{"caseRefNo":"","plaintiff":"","respondent":"","caseNote":"","noOfDates":"0","courtDetails":"","dates":[]}');
			
	//return JSON.parse('{"caseRefNo":"","plaintiff":"","respondent":"","caseNote":"","noOfDates":"0","dates":[]}');
}

function addDate(caseId, caseDate, dateNote) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	var dateObj = JSON.parse('{"dateId":"","caseDate":"","dateNote":""}');
	var noOfDates = parseInt(caseObj.noOfDates) + 1;
	dateObj.dateId = noOfDates.toString();
	dateObj.dateNote = dateNote;
	dateObj.caseDate = caseDate;
	
	if(caseObj.dates.length==0){
	caseObj.dates.push(dateObj);
	}
	else{
	var i= caseObj.dates.length-1;
	var dates =caseObj.dates;

	
	while(i>=0 && ((new Date(caseDate)) < (new Date(dates[i].caseDate)))){ 

		dates[i+1] = dates[i];
		i--;
	}
    dates[i+1] = dateObj;
	caseObj.dates=dates;
	}
	caseObj.noOfDates = noOfDates.toString();
	localStorage.setItem(caseId, JSON.stringify(caseObj));
	
	
}

function getDate(caseId, dateId) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	dates = caseObj.dates;
	for (var counter = 0; counter < dates.length; counter++) {
		if (dates[counter].dateId == dateId) {
			return dates[counter];
		}
	}
}

function updateDate(caseId, dateId, caseDate, dateNote) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	var dates = caseObj.dates;
	for (var counter = 0; counter < dates.length; counter++) {
		if (dates[counter].dateId == dateId) {
			dates.splice(counter, 1);
			break;
		}
	}

	var dateObj = JSON.parse('{"dateId":"","caseDate":"","dateNote":""}');
	dateObj.dateId = dateId;
	dateObj.dateNote = dateNote;
	dateObj.caseDate = caseDate;
	
	caseObj.dates.push(dateObj);
	localStorage.setItem(caseId, JSON.stringify(caseObj));

}

function deleteDate(caseId, dateId) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	var dates = caseObj.dates;
	for (var counter = 0; counter < dates.length; counter++) {
		if (dates[counter].dateId == dateId) {
		dates.splice(counter, 1);
			break;
		}
	}
	caseObj.dates = dates;
	localStorage.setItem(caseId, JSON.stringify(caseObj));
	
}

function dateExists(caseId, caseDate) {
	var caseObj = JSON.parse(localStorage.getItem(caseId));
	dates = caseObj.dates;
	for (var counter = 0; counter < dates.length; counter++) {
	
		
		if ((new Date(dates[counter].caseDate)).getTime() == (new Date(caseDate).getTime())) {
			return true;
		}
	}
	return false;
}